﻿namespace ACCOUNTING
{
    using ACCOUNTING.LVY.Business;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    [StandardModule]
    internal sealed class SQLBulkModules
    {
        public static void BulkDanhMuc(string strDanhMuc, string strConditions = "")
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM dbo." + strDanhMuc + ";", connection);
                long num = Convert.ToInt32(RuntimeHelpers.GetObjectValue(command.ExecuteScalar()));
                SqlDataReader reader = new SqlCommand("SELECT * FROM dbo." + strDanhMuc + " " + strConditions + " ;", connection).ExecuteReader();
                connectionStringClient = GetConnectionStringCenter();
                using (SqlConnection connection2 = new SqlConnection(connectionStringClient))
                {
                    connection2.Open();
                    using (SqlBulkCopy copy = new SqlBulkCopy(connection2))
                    {
                        copy.DestinationTableName = "dbo." + strDanhMuc;
                        try
                        {
                            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, "DELETE FROM dbo." + strDanhMuc + " " + strConditions, new IDataParameter[0]);
                            copy.WriteToServer(reader);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            MessageBox.Show(exception.Message);
                            ProjectData.ClearProjectError();
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
        }

        public static void BulkHoaDonBanHang(string strConditions)
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM dbo.ST_XuatKho;", connection);
                long num = Convert.ToInt32(RuntimeHelpers.GetObjectValue(command.ExecuteScalar()));
                SqlDataReader reader = new SqlCommand("SELECT * FROM dbo.ST_XuatKho " + strConditions + ";", connection).ExecuteReader();
                connectionStringClient = GetConnectionStringCenter();
                using (SqlConnection connection2 = new SqlConnection(connectionStringClient))
                {
                    connection2.Open();
                    using (SqlBulkCopy copy = new SqlBulkCopy(connection2))
                    {
                        copy.DestinationTableName = "dbo.ST_XuatKhoO";
                        try
                        {
                            copy.WriteToServer(reader);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            MessageBox.Show(exception.Message);
                            ProjectData.ClearProjectError();
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
        }

        public static void BulkHoaDonChiTiet(string strConditions)
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM dbo.ST_XuatKhoChiTiet;", connection);
                long num = Convert.ToInt32(RuntimeHelpers.GetObjectValue(command.ExecuteScalar()));
                SqlDataReader reader = new SqlCommand(" SELECT * FROM ST_XuatKhoChiTiet " + strConditions + ";", connection).ExecuteReader();
                connectionStringClient = GetConnectionStringCenter();
                using (SqlConnection connection2 = new SqlConnection(connectionStringClient))
                {
                    connection2.Open();
                    using (SqlBulkCopy copy = new SqlBulkCopy(connection2))
                    {
                        copy.DestinationTableName = "dbo.ST_XuatKhoChiTietO ";
                        try
                        {
                            copy.WriteToServer(reader);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            MessageBox.Show(exception.Message);
                            ProjectData.ClearProjectError();
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
        }

        public static void BulkNghiepVuBangCha(string BangCha, string TuNgay, string DenNgay, string BangCon = "", string FieldCha = "")
        {
            string connectionStringClient = GetConnectionStringClient();
            string commandText = "";
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM dbo." + BangCha + ";", connection);
                long num = Convert.ToInt32(RuntimeHelpers.GetObjectValue(command.ExecuteScalar()));
                SqlDataReader reader = new SqlCommand("SELECT * FROM dbo." + BangCha + " WHERE NgayChungTu Between '" + TuNgay + "' And '" + DenNgay + "'", connection).ExecuteReader();
                connectionStringClient = GetConnectionStringCenter();
                using (SqlConnection connection2 = new SqlConnection(connectionStringClient))
                {
                    connection2.Open();
                    using (SqlBulkCopy copy = new SqlBulkCopy(connection2))
                    {
                        copy.DestinationTableName = "dbo." + BangCha;
                        try
                        {
                            commandText = "DELETE FROM dbo." + BangCha + " WHERE NgayChungTu Between '" + TuNgay + "' and '" + DenNgay + "'";
                            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]);
                            copy.WriteToServer(reader);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            MessageBox.Show(exception.Message);
                            ProjectData.ClearProjectError();
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
            if (BangCon.Trim() != "")
            {
                BulkNghiepVuBangCon(BangCha, TuNgay, DenNgay, BangCon, FieldCha);
            }
        }

        public static void BulkNghiepVuBangCon(string BangCha, string TuNgay, string DenNgay, string BangCon, string FieldCha)
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM dbo." + BangCon + ";", connection);
                long num = Convert.ToInt32(RuntimeHelpers.GetObjectValue(command.ExecuteScalar()));
                SqlDataReader reader = new SqlCommand("SELECT * FROM dbo." + BangCon + " WHERE " + FieldCha + " IN (SELECT Khoa FROM " + BangCha + " WHERE NgayChungTu Between '" + TuNgay + "' And '" + DenNgay + "');", connection).ExecuteReader();
                connectionStringClient = GetConnectionStringCenter();
                using (SqlConnection connection2 = new SqlConnection(connectionStringClient))
                {
                    connection2.Open();
                    using (SqlBulkCopy copy = new SqlBulkCopy(connection2))
                    {
                        copy.DestinationTableName = "dbo." + BangCon;
                        try
                        {
                            DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.Text, "DELETE FROM dbo." + BangCon + " WHERE " + FieldCha + " IN (SELECT Khoa FROM " + BangCha + " WHERE NgayChungTu Between '" + TuNgay + "' and '" + DenNgay + "')", new IDataParameter[0]);
                            copy.WriteToServer(reader);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            MessageBox.Show(exception.Message);
                            ProjectData.ClearProjectError();
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
        }

        public static void DelDataFromClient(string strTable, string strConditions)
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                new SqlCommand("DELETE FROM dbo." + strTable + "  " + strConditions + ";", connection).ExecuteNonQuery();
            }
        }

        public static void DelHoaDonChiTietFromClient(string strConditions)
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                new SqlCommand("DELETE FROM dbo.ST_XuatKhoChiTiet " + strConditions + ";", connection).ExecuteNonQuery();
            }
        }

        public static void DelHoaDonFromClient(string strConditions)
        {
            string connectionStringClient = GetConnectionStringClient();
            using (SqlConnection connection = new SqlConnection(connectionStringClient))
            {
                connection.Open();
                new SqlCommand("DELETE FROM dbo.ST_XuatKho " + strConditions + ";", connection).ExecuteNonQuery();
            }
        }

        private static string GetConnectionStringCenter()
        {
            return ("Data Source=" + modGeneral.H_DBSERVERNAME + ";Initial Catalog=" + modGeneral.H_DBDATANAME + ";User Id=" + modGeneral.H_DBUSER + ";Password=" + modGeneral.H_DBPASS + ";");
        }

        private static string GetConnectionStringClient()
        {
            clsDataChiNhanh nhanh = new clsDataChiNhanh();
            nhanh.Load("01");
            string tenMayChu = "";
            string dbName = "";
            string dbUser = "";
            string dbPass = "";
            tenMayChu = nhanh.TenMayChu;
            dbName = nhanh.DbName;
            dbUser = nhanh.DbUser;
            dbPass = nhanh.DbPass;
            return ("Data Source=" + tenMayChu + ";Initial Catalog=" + dbName + ";User Id=" + dbUser + ";Password=" + dbPass + ";");
        }

        public static void TranferDanhMuc(string strDanhMuc, string strConditions = "")
        {
            string connectionStringCenter = GetConnectionStringCenter();
            using (SqlConnection connection = new SqlConnection(connectionStringCenter))
            {
                connection.Open();
                SqlCommand command = new SqlCommand("SELECT COUNT(*) FROM dbo." + strDanhMuc + ";", connection);
                long num = Convert.ToInt32(RuntimeHelpers.GetObjectValue(command.ExecuteScalar()));
                SqlDataReader reader = new SqlCommand("SELECT * FROM dbo." + strDanhMuc + " " + strConditions + " ;", connection).ExecuteReader();
                connectionStringCenter = GetConnectionStringClient();
                using (SqlConnection connection2 = new SqlConnection(connectionStringCenter))
                {
                    connection2.Open();
                    using (SqlBulkCopy copy = new SqlBulkCopy(connection2))
                    {
                        copy.DestinationTableName = "dbo." + strDanhMuc;
                        try
                        {
                            DelDataFromClient(strDanhMuc, strConditions);
                            copy.WriteToServer(reader);
                        }
                        catch (Exception exception1)
                        {
                            ProjectData.SetProjectError(exception1);
                            Exception exception = exception1;
                            MessageBox.Show(exception.Message);
                            ProjectData.ClearProjectError();
                        }
                        finally
                        {
                            reader.Close();
                        }
                    }
                }
            }
        }
    }
}

